﻿using MultiAgentsServer.Pages;
using OpenAI.ObjectModels;
using System.Text.Json;

namespace MultiAgentsServer.Scripts.Shared;

public class DefaultModelConfigModel
{
    public class ModelConfig
    {
        public string LLMSwitchNodeModel { get; set; }
        public string ParameterExtractionNodeModel { get; set; }
        public string RenameChatRoomModel { get; set; }
        public string ReplyAssistantModel { get; set; }
        public string DefaultAgentModel { get; set; }
        public string GitOperatorAgentModel { get; set; }
        public string OctoHelperAgentModel { get; set; }
    }

    private static ModelConfig _defaultModelConfig;

    public static ModelConfig DefaultModelConfig
    {
        get
        {
            if (_defaultModelConfig == null)
            {
                InitializeDefaultModelConfigRoot();
            }
            return _defaultModelConfig;
        }
    }
    private static void InitializeDefaultModelConfigRoot()
    {
        string jsonConfigData = ModelConfigurationModel.LoadConfiguration();
        if (string.IsNullOrEmpty(jsonConfigData))
        {
            _defaultModelConfig = new ModelConfig
            {
                LLMSwitchNodeModel = Models.EnumToString(Models.Model.Gpt_4o),
                ParameterExtractionNodeModel = Models.EnumToString(Models.Model.Gpt_4o),
                RenameChatRoomModel = Models.EnumToString(Models.Model.Gpt_4o),
                ReplyAssistantModel = Models.EnumToString(Models.Model.Gpt_4o),
                DefaultAgentModel = Models.EnumToString(Models.Model.Gpt_4o),
                GitOperatorAgentModel = Models.EnumToString(Models.Model.Gpt_4o),
                OctoHelperAgentModel = Models.EnumToString(Models.Model.Gpt_4o)
            };

            jsonConfigData = JsonSerializer.Serialize(_defaultModelConfig);
            ModelConfigurationModel.StoreConfiguration(jsonConfigData);
        }
        else
        {
            _defaultModelConfig = JsonSerializer.Deserialize<ModelConfig>(jsonConfigData);
        }
    }
}
